#include "src/antistress.h"
#include <QtGui>
#include <QApplication>
#include "OakSensorVisual.h"
#include <QtCore\qdebug.h>
#include "analyser.h"

#include <qwt_plot.h>

int main(int argc, char *argv[])
{
	/*
	int sensors = -1;
	    int fftPoints = 30;
	    int logFolder = 0;


	    for (int i=1; i<argc; i++)
	    {
	        if (strcmp(argv[i], "-s") == 0 && i<(argc+1))
	            sensors = atoi(argv[i+1]);

	        if (strcmp(argv[i], "-p") == 0 && i<(argc+1))
	            fftPoints = atoi(argv[i+1]);

	        if (strcmp(argv[i], "-l") == 0 && i<(argc+1))
	            logFolder = i+1;
	    }

	    QApplication a(argc, argv);

	    sensorPlot w;

	    // initialize the controller
	    Analyser* pAnalyser = new Analyser(fftPoints);
	    pAnalyser->setView(&w);

	    vector<UsbSensor*> sensorList;

	    // Sensor autodetection
	    if (sensors < 0)
	    {
	        for (int i=0; i<10; i++)
	        {
	            char devName[256];
	            sprintf(devName, "/dev/usb/hiddev%d", i);
	            if (!UsbSensor::checkForSensor(devName))
	                break;

	            UsbSensor* pSensor = new UsbSensor();
	            cout << "*******************************" << endl << "Sensor " << i << endl << "*******************************" << endl;
	            pSensor->initSensor(devName);
	            pSensor->startCapture();
	            pAnalyser->addSensor(pSensor);
	            sensorList.push_back(pSensor);
	        }
	    }

	    // Sensor fixed number
	    else
	    {
	        for (int i=0; i<sensors; i++)
	        {
	            char devName[256];
	            sprintf(devName, "/dev/usb/hiddev%d", i);
	            UsbSensor* pSensor = new UsbSensor();
	            pSensor->initSensor(devName);
	            pSensor->startCapture();
	            pAnalyser->addSensor(pSensor);
	            sensorList.push_back(pSensor);
	        }
	    }

	    // initialize the view
	   w.show();
	    int ret = a.exec();

	    // terminate all
	    delete pAnalyser;
	    for (unsigned int i=0; i<sensorList.size(); i++)
	    {
	        sensorList[i]->stop();
	        delete sensorList[i];
	    }

	    return ret;*/


	/*	int x;
	tOakSensor sensor;
	Oak_FindSensor(0x0000,0x0000,"T93170015",NULL,NULL,NULL,NULL,&sensor);


	HANDLE handle = CreateFile(sensor.DevicePath,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	

		signed char buff[256];
	DWORD number;

	
    vector<int> values;
	while(true){
		int count=0;
		while (count<4){
			ReadFile(handle,buff,sizeof(buff),&number,NULL);
			unsigned int const kCount = number / 2;
			//cout << "Status:"<<GetLastError()<<" number:"<<number<<" kCount:"<<kCount<<endl;
			if(4 != values.size()) {
			  values.resize(4);   
			}
			for (unsigned int i =0; i < 4; i++){
				//cout << buff[i]<<endl;
				if(i!=0){
					values.at(i) =(buff[1+2*i]+256*buff[2+2*i]);
				}else{
					values.at(i) =  (unsigned char)buff[1+2*i]+256*buff[2+2*i];
				}
			}
	
			if (number>0){
				for (unsigned int i=0; i<values.size() && count<4; i++, count++){
			//        m_pBuffers[count].append(values[i]);
					qDebug()<< QString("Chan%1: %2").arg(i).arg(values[i]*0.001);
				}
			}
		}

	}*/

    QApplication a(argc, argv);
	
	int fftPoints = 30;
	sensorPlot w;
	// initialize the controller
	Analyser* pAnalyser = new Analyser(fftPoints);
	pAnalyser->setView(&w);
	OakSensorVisual *usbsensor = new OakSensorVisual("T93170015");
	usbsensor->startCapture();

	pAnalyser->addSensor(usbsensor);

	w.show();

	
    Antistress w2;
    w2.show();
	
	return a.exec();
}
